﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineNewsEditorial_DaoLibrary.Interfaces;
using Model.Context;
using System.Data.Linq;

namespace OnlineNewsEditorial_DaoLibrary.Impl
{
    public abstract class AbstractDao<Obj> : IDao<Obj>// where Obj : class
    {
        protected OnlineNewsEditorialDataContext _ctx;

        public AbstractDao()
        {
            _ctx = new OnlineNewsEditorialDataContext();
        }

        public AbstractDao(DataContext dc)
        {
            _ctx = (OnlineNewsEditorialDataContext)dc;
        }

        public bool create(Obj obj)
        {
            try
            {
                _ctx.GetTable<Obj>().InsertOnSubmit(obj);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool update(Obj obj)
        {
            try
            {
                _ctx.GetTable<Obj>().Attach(obj);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool delete(Obj obj)
        {
            try
            {
                _ctx.GetTable<Obj>().DeleteOnSubmit(obj);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public abstract List<Obj> getAll();
    }
}
